'use strict';

//加载新闻
// function topic() {
//     $.server({ categoryId: 20, pageSize: 6, pageNum: 1 }, '/website/v1/queryCategoryContent').then(function (res) {
//         var obj = res.result.data;
//         var html = '',
//             length = obj.length > 6 ? 6 : obj.length;
//         for (var i = 0; i < length; i++) {
//             if (i == 0) {
//                 if (obj[i].description && obj[i].description.length > 80) {
//                     obj[i].description = obj[i].description.substring(0, 80) + "...";
//                 }
//                 html += ' <a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u884C\u4E1A\',\'\u884C\u4E1A\u6CD5\u89C4\')">\n        <div class="new-title-box col-md-12 col-sm-12 col-xs-12">\n            <div class="new-title">' + obj[i].title + '</div>\n            <div class="new-content-box">\n                <img class="new-img img-responsive" src="' + obj[i].poster + '"></img>\n                <div class="new-word">\n                ' + obj[i].description + '\n                </div>\n            </div>\n        </div>\n    </a>';
//             } else {
//                 html += '<a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u884C\u4E1A\',\'\u884C\u4E1A\u6CD5\u89C4\')"><div class="topic-box title-box col-md-12 col-sm-12 col-xs-12">\n      <div class="flex-box">\n      <div class="around"></div>\n      <div class="topic-title">' + obj[i].title + '</div>\n      </div>\n      <div class="more">' + obj[i].createTime.substring(5, 10) + '</div>\n      </div></div>';
//             }
//         }
//         $("#topicContent").html(html);
//     });
// }

//点击跳转新闻详情
function industryDetail() {
    window.open("industryDetail.html");
}

//模拟
// function marketIndex() {
//     var obj = [{ title: '从业指数', sum: '2840.28', percentageL: '-2.16', percentageR: '-2.16', content: '从业指数是现代货币主义者为减轻通货膨胀的副作用而提出的一项实行对工资、物价的冻结、管制政策的措施。收入指数化是以凯恩斯主义的 “收入政策” 的对立面出现的。', id: '344' }, { title: '收入指数', sum: '13729.33', percentageL: '+32.27', percentageR: '-2.11%', content: '收入指数是现代货币主义者为减轻通货膨胀的副作用而提出的一项实行对工资、物价的冻结、管制政策的措施。收入指数化是以凯恩斯主义的 “收入政策” 的对立面出现的。', id: '344' }, { title: '投保指数', sum: '2049.38', percentageL: '+10.39', percentageR: '-1.47%', content: '投保指数是现代货币主义者为减轻通货膨胀的副作用而提出的一项实行对工资、物价的冻结、管制政策的措施。收入指数化是以凯恩斯主义的 “收入政策” 的对立面出现的。', id: '344' }];
//     var html = '',
//         length = obj.length > 10 ? 10 : obj.length;

//     for (var i = 0; i < length; i++) {
//         html += '<div class="industry-w-box col-md-12 col-sm-12 col-xs-12">\n    <div class="industry-data-box">\n        <div class="f-box ">\n            <div class="title">' + obj[i].title + '</div>\n            <div class="title">' + obj[i].sum + '</div>\n            <div class="title">' + obj[i].percentageL + '&nbsp;&nbsp;' + obj[i].percentageR + '</div>\n        </div>\n        <div class="s-box "><div id=\'main' + i + '\' style="width: 100%;height:210px;"></div></div>\n        <div class="t-box ">\n            <div class="practice">' + obj[i].title + '\uFF1A</div>\n            <div class="practice-content">' + obj[i].content + '</div>\n        </div>\n    </div>\n</div>';
//     }
//     $("#marketIndex").html(html);
// }

//模拟加载研究成果
// function researchFindings() {
//     $.server({ categoryId: 21, pageSize: 6, pageNum: 1 }, '/website/v1/queryCategoryContent').then(function (res) {
//         console.log(res, "专题报告");
//         var obj = res.result.data;
//         var html = '',
//             length = obj.length > 6 ? 6 : obj.length;
//         for (var i = 0; i < length; i++) {
//             if (i == 0) {
//                 html += ' <a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u884C\u4E1A\',\'\u884C\u4E1A\u8D44\u8BAF\')">\n        <div class="new-title-box col-md-12 col-sm-12 col-xs-12">\n            <div class="new-title">' + obj[i].title + '</div>\n            <div class="new-content-box">\n                <img class="new-img img-responsive" src="' + obj[i].poster + '"></img>\n                <div class="new-word">\n                ' + obj[i].description + '\n                </div>\n            </div>\n        </div>\n    </a>';
//             } else {
//                 html += '<a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u884C\u4E1A\',\'\u884C\u4E1A\u8D44\u8BAF\')"><div class="topic-box title-box col-md-12 col-sm-12 col-xs-12">\n      <div class="flex-box">\n      <div class="around"></div>\n      <div class="topic-title">' + obj[i].title + '</div>\n      </div>\n      <div class="more">' + obj[i].createTime.substring(5, 10) + '</div>\n      </div></div>';
//             }
//         }
//         $("#researchFindings").html(html);
//     });
// }

//加载专家观点
// function researchFindings1() {
//     // lecturerId待定
//     var params = { contentType: 1, pageSize: 100, pageNum: 1 };
//     $.server(params, '/website/v1/lecturerContent').then(function (res) {
//         console.log(res, "专家观点");
//         var obj = res.result.data;
//         var html = '',
//             length = obj.length > 6 ? 6 : obj.length;
//         for (var i = 0; i < length; i++) {
//             if (!obj[i].createTime) {
//                 obj[i].createTime = '';
//             }
//             html += '<a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u4E13\u5BB6\',\'\u4E13\u5BB6\u89C2\u70B9\')"><div class="topic-box title-box col-md-12 col-sm-12 col-xs-12">\n    <div class="flex-box">\n    <div class="around"></div>\n    <div class="topic-title">' + obj[i].title + '</div>\n    </div>\n    <div class="more">' + obj[i].createTime.substring(5, 10) + '</div>\n    </div></div>';
//         }
//         $("#researchFindings1").html(html);
//     });
// }
/**
 * 跳转到文章详情
 */
function goLessonDetail(id, word, childTitle) {
    console.log(id, word, childTitle);
    location.href = 'industryDetail.html?id=' + id + '&word=' + word + '&childTitle=' + childTitle;
} 

// 页面顶部大图数据(待定)
function posterImg() {
    $.server({ location: 3 }, '/website/v1/getPoster').then(function (res) { 
        $('#bannerImg').attr("src", res.result.data[0].url);
    });
}

function getNewsData(){ 
    $.server({ categoryId : 18 }, '/website/v1/queryNavCategory').then(function (res) {
      var mainList=res.result.main;
      var rightList=res.result.right;
      loadMain(mainList);
      loadRight(rightList);

    });
  }
  //加载主新闻
  function loadMain(mainList){
    var html=''; 
    for(var i=0 ; i<mainList.length ; i++){
      if(i==mainList.length-1){
        html+= '<span class="expertSpan " data-index="'+mainList[i].categoryId+'" data-name="'+mainList[i].categoryName+'">'+mainList[i].categoryName+'</span>';
      }else {
        html+= '<span class="expertSpan" data-index="'+mainList[i].categoryId+'" data-name="'+mainList[i].categoryName+'">'+mainList[i].categoryName+'</span><span class="title-c xiegang">/ </span>';
      }
    }
    $(".expert-title-box").html(html);
    $(".expertSpan").eq(0).addClass("title-c");
    $(".expertSpan").click(function(e){
      var index=e.currentTarget.dataset.index;
      var name=e.currentTarget.dataset.name;
      getPageData(index,name);
      $(".expertSpan").removeClass("title-c");
      $(this).addClass("title-c");
    }) 
    getPageData(mainList[0].categoryId,mainList[0].categoryName);
  }
  function loadRight(rightList){
    var html=''; 
    $(".rightList").text(rightList[0].categoryName);
    getPageData(rightList[0].categoryId,rightList[0].categoryName);
    getPageData(rightList[1].categoryId,rightList[1].categoryName);
  }
  
  function getPageData(categoryId,name){ 
    $.server({ categoryId : categoryId }, '/website/v1/queryCategoryContent').then(function (res) {
      var detail=res.result.data;
      if(categoryId== 19 || categoryId==23 || categoryId==27 || categoryId==37 || categoryId==38  || categoryId==39){ 
        var mainListHtml='';
        for(var i=0;i<detail.length;i++){
          mainListHtml+=' <div class="list-box" onclick="goLessonDetail(' + detail[i].contentId + ',\'\u7ECF\u6D4E\',\''+name+'\')">'+
          '<div  style="width: 236px;" class="list_shy">'+
          '<div class="ani_imgBox"><img src="'+detail[i].poster+'" style="width: 236px;height: 144px;" alt=""></div>'+
          '</div>'+
          '<div class="right-box">'+
              '<div class="expertDetail-title-box">'+
                  '<div class="title">'+detail[i].title+'</div>'+ 
              '</div>'+
              '<div class="expertDetail-constent">'+detail[i].description+'</div>'+
              '<div class="list-bottom-box">'+
                  '<div>'+detail[i].createTime+'</div>'+
                  '<div class="time-box">'+
                      '<img src="images/expertDetail/fans-2.png" style="width: 16px;height: 16px;margin-right: 5px;">'+
                      '<div>'+detail[i].commentCount+'</div>'+
                  '</div>'+
              '</div>'+
            '</div>'+
          '</div>'
        }
        $("#mainList").html(mainListHtml);
      }else if(categoryId==40){ //热文
        var html='';
        for(var i=0;i<detail.length;i++){
          var time=timeUplc(detail[i].createTime);
          if(i==0){
            html+='<div class="cont-box"  onclick="goLessonDetail(' + detail[i].contentId + ',\'\u7ECF\u6D4E\',\''+name+'\')">'+
                '<div class="ani_imgBox"><img src="' + detail[i].poster + '" style="width: 132px;height: 80px"> </div>'+
                '<div  class="one-img rankNum hot">'+(i+1)+'</div>'+
                '<div class="word-industry" >' + detail[i].title + '</div>'+
            '</div>';
          }else{
            html+='<div class="cont-box cont-box-last"  onclick="goLessonDetail(' + detail[i].contentId + ',\'\u7ECF\u6D4E\',\''+name+'\')">'+
                '<div  class="rankNum rank'+i+'">'+(i+1)+'</div>'+
                '<div class="word-industry-last">' + detail[i].title + '</div>'+
            '</div>'
          }
          
        }
        $("#rewen_hot").after(html); 
      } 
      
    });
  }
  
  function timeUplc(time){
    var time1=new Date(time)/1000;
    var time=new Date()/1000;
    if(time<time1){ return 0 +"分钟前"}
    else {
      if(time-time1>24*60*60){//大于一天
        var day=parseInt((time-time1)/(24*60*60));
        return day+"天前";
      }else if(time-time1>3600){
        var hour = parseInt((time-time1)/(60*60));
        return hour+"小时前";
      }else{
        var miniour=parseInt((time-time1)/(60));
        return miniour+"分钟前";
      }
    }
  }

/**
 * 页面初始化时加载 window.onload
 */
$(function () {
    //调用server加载首页新闻
    posterImg();
    // topic();
    // marketIndex();
    // researchFindings();
    // researchFindings1();

    getNewsData();

    $("#topicContent .topic-title").click(function (e) {
        var index = e.currentTarget.dataset.index;
        console.log(e);
        if (index == 1) {} else if (index == 2) {} else if (index == 3) {}
    });

    $("#researchFindings .topic-title").click(function (e) {
        var index = e.currentTarget.dataset.index;
        if (index == 1) {} else if (index == 2) {} else if (index == 3) {}
    });

    // 基于准备好的dom，初始化echarts实例
    // for (var i = 0; i < 3; i++) {
    //     var myChart = echarts.init(document.getElementById('main' + i));

    //     // 指定图表的配置项和数据
    //     arr = [{
    //         xAxis: {
    //             type: 'category',
    //             boundaryGap: false
    //         },
    //         legend: {
    //             data: ['从业指数']
    //         },
    //         yAxis: {
    //             type: 'value',
    //             boundaryGap: [0, '30%']
    //         },
    //         visualMap: {
    //             type: 'piecewise',
    //             show: false,
    //             dimension: 0,
    //             seriesIndex: 0,
    //             pieces: [{
    //                 gt: 1,
    //                 lt: 3,
    //                 color: '#FFAB8C'
    //             }, {
    //                 gt: 5,
    //                 lt: 7,
    //                 color: '#FFAB8C'
    //             }]
    //         },
    //         series: [{
    //             name: '从业指数',
    //             type: 'line',
    //             smooth: 0.6,
    //             symbol: 'none',
    //             lineStyle: {
    //                 color: '#FFAB8C',
    //                 width: 5
    //             },
    //             markLine: {
    //                 symbol: ['none', 'none'],
    //                 label: { show: false },
    //                 data: [{ xAxis: 1 }, { xAxis: 3 }, { xAxis: 5 }, { xAxis: 7 }]
    //             },
    //             areaStyle: {},
    //             data: [['2019-01', 200], ['2019-02', 400], ['2019-03', 650], ['2019-04', 500], ['2019-05', 250], ['2019-06', 300], ['2019-07', 450], ['2019-08', 300], ['2019-09', 100], ['2019-10', 100], ['2019-11', 100], ['2019-12', 100]]
    //         }]
    //     }, {
    //         xAxis: {
    //             type: 'category',
    //             boundaryGap: false
    //         },
    //         legend: {
    //             data: ['收入指数']
    //         },
    //         yAxis: {
    //             type: 'value',
    //             boundaryGap: [0, '30%']
    //         },
    //         visualMap: {
    //             type: 'piecewise',
    //             show: false,
    //             dimension: 0,
    //             seriesIndex: 0,
    //             pieces: [{
    //                 gt: 1,
    //                 lt: 3,
    //                 color: '#67B5FF'
    //             }, {
    //                 gt: 5,
    //                 lt: 7,
    //                 color: '#67B5FF'
    //             }]
    //         },
    //         series: [{
    //             name: '收入指数',
    //             type: 'line',
    //             smooth: 0.6,
    //             symbol: 'none',
    //             lineStyle: {
    //                 color: '#67B5FF',
    //                 width: 5
    //             },
    //             markLine: {
    //                 symbol: ['none', 'none'],
    //                 label: { show: false },
    //                 data: [{ xAxis: 1 }, { xAxis: 3 }, { xAxis: 5 }, { xAxis: 7 }]
    //             },
    //             areaStyle: {},
    //             data: [['2019-01', 200], ['2019-02', 400], ['2019-03', 650], ['2019-04', 500], ['2019-05', 250], ['2019-06', 300], ['2019-07', 450], ['2019-08', 300], ['2019-09', 100], ['2019-10', 100], ['2019-11', 100], ['2019-12', 100]]
    //         }]
    //     }, {
    //         xAxis: {
    //             type: 'category',
    //             boundaryGap: false
    //         },
    //         legend: {
    //             data: ['投保指数']
    //         },
    //         yAxis: {
    //             type: 'value',
    //             boundaryGap: [0, '30%']
    //         },
    //         visualMap: {
    //             type: 'piecewise',
    //             show: false,
    //             dimension: 0,
    //             seriesIndex: 0,
    //             pieces: [{
    //                 gt: 1,
    //                 lt: 3,
    //                 color: '#BEA6FF'
    //             }, {
    //                 gt: 5,
    //                 lt: 7,
    //                 color: '#BEA6FF'
    //             }]
    //         },
    //         series: [{
    //             name: '投保指数',
    //             type: 'line',
    //             smooth: 0.6,
    //             symbol: 'none',
    //             lineStyle: {
    //                 color: '#BEA6FF',
    //                 width: 5
    //             },
    //             markLine: {
    //                 symbol: ['none', 'none'],
    //                 label: { show: false },
    //                 data: [{ xAxis: 1 }, { xAxis: 3 }, { xAxis: 5 }, { xAxis: 7 }]
    //             },
    //             areaStyle: {},
    //             data: [['2019-01', 200], ['2019-02', 400], ['2019-03', 650], ['2019-04', 500], ['2019-05', 250], ['2019-06', 300], ['2019-07', 450], ['2019-08', 300], ['2019-09', 100], ['2019-10', 100], ['2019-11', 100], ['2019-12', 100]]
    //         }]
    //     }];
    //     option = arr[i];

    //     // 使用刚指定的配置项和数据显示图表。
    //     myChart.setOption(option);
    // }
});